package com.hlm.demo.controller.admininfo;

import com.hlm.demo.common.ResultWrapper;
import com.hlm.demo.entity.AdminInfo;

import com.hlm.demo.entity.ex.AdminWithRole;
import com.hlm.demo.service.admininfo.IAdminInfoService;
import com.hlm.demo.validator.admininfovalidator.AdminAddValidator;
import com.hlm.demo.validator.admininfovalidator.AdminUpdValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;



@Controller
@RequestMapping("/admin")

public class AdminInfoController {

//    @Autowired
//    private IAdminInfoService adminInfoService;

    private static IAdminInfoService adminInfoService;

    @Autowired
    public AdminInfoController(IAdminInfoService adminInfoService){
        AdminInfoController.adminInfoService = adminInfoService;
    }


    // 查询所有管理员的信息和他们所对应的角色
    @RequestMapping("/adminList")
    @ResponseBody
    public ResultWrapper findAll(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "2") Integer count,
                                 String search,
                                 @RequestParam(defaultValue = "0") Integer moduleId) {

        System.out.println("看看搜索栏的内容:" + search);
                List<AdminWithRole> adminInfoList = adminInfoService.findAll(page,count,search,moduleId);
                int totalCount = adminInfoService.totalCount(search,moduleId);
        System.out.println("我收到了搜索的模块的id: ------>" + moduleId);
                return ResultWrapper.success(totalCount, adminInfoList);

    }

    // 添加管理员
    @RequestMapping("/adminAdd")

    public String addAdmin(@Validated(value = {AdminAddValidator.class}) AdminInfo adminInfo, BindingResult result, String allRole){
        if (result.hasErrors()){
            List<FieldError> errors = result.getFieldErrors();
            System.out.println("有错误啊! *********  ******");
            errors.forEach(System.out::println);
        }else{
        adminInfoService.addOneAdmin(adminInfo,allRole);
        }
        return "redirect:admin_list.html";

    }

    // 修改某个管理员的信息
    @RequestMapping("/adminModi")

    public String updateAdmin(@Validated(value = {AdminUpdValidator.class}) AdminInfo adminInfo, BindingResult result , String allRole){
        if (result.hasErrors()){
            System.out.println("修改这里有错误啊 ******* ***** * * *");
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(System.out::println);
        } else{

        adminInfoService.updateAdmin(adminInfo,allRole);
        }
        return "redirect:admin_list.html";
    }


    // 删除某个管理员
    @RequestMapping("/adminDel")
    public void delAdmin(Integer adminId){
        adminInfoService.deleteAdmin(adminId);
    }



}
